package com.byCreation.Profile;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.byCreatetion.adabter.TglobeAppAdapter;
import com.byCreation.Control.Control;
import com.byCreation.t_globe.R;
import com.google.analytics.tracking.android.EasyTracker;
import com.google.analytics.tracking.android.GoogleAnalytics;
import com.google.analytics.tracking.android.Tracker;



import Myclass.AllIntent;
import Myclass.ConnectInternet;
import Parser.HttpGetJson;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class TglobeAppActivity extends Activity implements OnClickListener,OnItemClickListener{

	private GridView gridView;
	private ArrayList<HashMap<String, Object>> MyArrList;
	private HashMap<String, Object> map = new HashMap<String, Object>();
	private ProgressDialog mProgressDialog;
	private TglobeAppAdapter adapter;
	
	
	AllIntent allintent=new AllIntent(this);
	
	
	 private String[] id_img={"http://www.walkin-store.com/wp/wp-content/uploads/2013/09/LINE_icon.png"
			 /*,"URL_img2"
			 ,  "URL_img3"*/};
	 
	 
	 private String[] id_url={"jp.naver.line.android"
			 ,"jp.naver.line.android"
			 , "jp.naver.line.android"
			 , "jp.naver.line.android"
			 , "jp.naver.line.android"
			 , "jp.naver.line.android"
			 , "jp.naver.line.android"
			 , "jp.naver.line.android"};

	// ===============GoogleAnalytics==============
		private GoogleAnalytics mGaInstance;
		private Tracker mGaTracker;
		// =============================================
		// ===================================googleAnalytics=============================
	@Override
	public void onStart() {
		super.onStart();
		// The rest of your onStart() code.
		EasyTracker.getInstance().activityStart(this); // Add this method.
		mGaTracker.sendView("TglobeApp Screen");

	}

	@Override
	public void onStop() {
		super.onStop();
		// The rest of your onStop() code.
		EasyTracker.getInstance().activityStop(this); // Add this method.

	}

	// =====================================================================	 
	 
	 
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_tglobe_app);
	
		// =============================GoogleAnalytics=========================
		mGaInstance = GoogleAnalytics.getInstance(this);
		mGaTracker = mGaInstance.getTracker(Control.IDGoogleanalytics);		
		
		
		
		
	final ImageView img_close = (ImageView) findViewById(R.id.img_close);
		 img_close.setOnClickListener(new OnClickListener() {
             public void onClick(View v) {
                 // Perform action on click   
            	 finish();
               
             }
         });
		 
		 
		 
		 
			SetListView();
		 
	}

	private void SetListView()
    {
		
		
		
		
    	
		gridView = (GridView)findViewById(R.id.gridView1);
			this.setProgressBarIndeterminateVisibility(true);
			gridView.setOnItemClickListener(this);
			 //=====================ProgressDialog==========================
	        mProgressDialog = new ProgressDialog(this);
	        mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);         
	        mProgressDialog.setMessage("Loading......");                       
	        mProgressDialog.setCancelable(false);  

		
	
			MyArrList = new ArrayList<HashMap<String, Object>>();

		
		if(new ConnectInternet(this).checkInternetConnection())
		new LoadData().execute();
		
    }

	@Override
	public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
		// TODO Auto-generated method stub
		//MyArrList.get(position).get("VdoId").toString();
		allintent.OpenGooglePlay(MyArrList.get(position).get("url").toString());
	}

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		
	}
	
	
	
	class LoadData extends AsyncTask<Object, Integer, Object> {

		
		
		
		
		private boolean onCancelled=false;
		private ProgressDialog mProgressDialog;
		
		@Override
		protected void onCancelled() {
			// TODO Auto-generated method stub
			onCancelled=true;
			super.onCancelled();
		}
		
		@SuppressWarnings("null")
		@Override
		protected Object doInBackground(Object... params) {

			
			
			
				 
				 try {
						
						JSONObject jsonObj=null;
						jsonObj = new JSONObject(HttpGetJson.GetJson(Control.getAPI_T_GLOBE_APP(),Control.user,Control.pass));
						JSONArray jsonarray_data=jsonObj.getJSONArray("tglobe_app");
							
						
						 for (int i = 0; i <jsonarray_data.length(); i++)
						 { 
							 
							 map = new HashMap<String, Object>();
							 JSONObject jsonObj_data=jsonarray_data.getJSONObject(i);


							 map.put("title",jsonObj_data.getString("app_name"));
							 map.put("detail",jsonObj_data.getString("desc"));
							 map.put("url",jsonObj_data.getString("path"));
							 map.put("img",jsonObj_data.getString("img"));
							
			
						     MyArrList.add(map);
						 }
						
						
						 
						 
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				 
			
				 
			return null;
		}

		@Override
		protected void onPreExecute() {

			
			
			 //=====================ProgressDialog==========================
	        mProgressDialog = new ProgressDialog(TglobeAppActivity.this);
	        mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);         
	        mProgressDialog.setMessage("Loading......");                       
	    	mProgressDialog.setCancelable(true);
		
			
			if(mProgressDialog!=null)
				mProgressDialog.show();
			
			
			adapter=null;
			
			if (adapter == null) {
				adapter = new TglobeAppAdapter(TglobeAppActivity.this, MyArrList);
				gridView.setAdapter(adapter);
				
				Log.e("TG", "adapter = new NotiListAdapter(getActivity(), MyArrList);");
				
			}

			
			super.onPreExecute();
		}

		@Override
		public void onProgressUpdate(Integer... progress) {

			Log.e("star", "progressupdate");
			
			
		}

		@Override
		protected void onPostExecute(Object result) {
		
			
			
			
			if(!onCancelled)
			{
				runOnUiThread (new Thread(new Runnable() { 
			         public void run() {
			                adapter.notifyDataSetChanged();      
			                
	
							Log.e("TG", " adapter.notifyDataSetChanged();  ");
			         }
			     }));
				
				 
			}
			
			if(mProgressDialog!=null)
				mProgressDialog.hide();
			 
			
		}

	}



}
